<?php
include 'sql_config.php';

if(isset($_REQUEST['id'])){
    $id = $_REQUEST['id'];

    $sql = "SELECT * FROM rent WHERE id=$id";
    $res = $conn->query($sql);
    if($res->num_rows>0){
        while($rows = $res->fetch_assoc()){
            $name = $rows['name'];
            $w = $rows['w'];
            $jiage = $rows['jiage'];
            $start_time = $rows['start_time'];
            $deathline = $rows['deathline'];
        echo <<< EOF
        <!DOCTYPE html>
        <html>
            <head>
                <title>update</title>
                <meta charset="UTF-8">
            </head>
            <body>
                <div>
                <form method="post" action="update.php">
                    <label>租赁者</label><input name="new_name" value="$name">
                    <label>租赁物品</label><input name="new_w" value="$w">
                    <label>租赁价格</label><input name="new_jiage" value="$jiage">
                    <label>开始时间</label><input name="new_start_time" type='date' value="$start_time">
                    <label>结束时间</label><input name="new_deathline" type='date' value="$deathline">
                    <input name="id" value="$id" type="hidden">
                    <button class='button_b'>更新</button>
                </form>
                <button class='button_b' onclick="location.href=('home.php');">取消</button>
                </div>
            </body>
            <style>
            .button_b{
            color: white;
            background-color: blue;
            border: none;
            border-radius: 5px;
            }
            div,form{
                display:flex;
                flex-direction: column;
            }
            </style>
        </html>
    EOF;
        }
    }else{
    echo("查询失败");
    }
}



if(isset($_REQUEST['new_name'])){
    $id = $_REQUEST['id'];
    $new_name = $_REQUEST['new_name'];
    $new_w = $_REQUEST['new_w'];
    $new_jiage = $_REQUEST['new_jiage'];
    $new_start_time = $_REQUEST['new_start_time'];
    $new_deathline = $_REQUEST['new_deathline'];

    $sql = "UPDATE rent SET name='$new_name',w='$new_w',jiage='$new_jiage',start_time='$new_start_time',deathline='$new_deathline' 
    WHERE id=$id";

    if($conn->query($sql) === TRUE){
        die("更新成功");
    }else{
        echo("更新失败");
    }
}


?>

